import com.sun.xml.internal.bind.v2.model.core.EnumConstant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/showRequest")
public class showRequestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //这里设置响应的Contnt-type,告诉浏览器，响应body里面的数据的类型是什么样子的
        resp.setContentType("text/html");

        //搞个StringBuilder把这个api拼接到一起，一起返回到响应中
        StringBuilder sb=new StringBuilder();
//        sb.append(req.getProtocol()+"  ");
//        sb.append(req.getMethod()+"  ");
//        sb.append(req.getRequestURI()+"  ");
//        sb.append(req.getContextPath()+"  ");
//        sb.append(req.getQueryString()+"  ");
        sb.append(req.getProtocol());
        //设置一个换行符
        sb.append("<br>");
        sb.append(req.getMethod());
        sb.append("<br>");
        sb.append(req.getRequestURI());
        sb.append("<br>");
        sb.append(req.getContextPath());
        sb.append("<br>");
        sb.append(req.getQueryString());
        sb.append("<br>");
        sb.append("<br>");
        sb.append("<br>");
        sb.append("<br>");

        //获取到header中所有的键值对
        //枚举获取
       Enumeration<String> headernames=req.getHeaderNames();
       while (headernames.hasMoreElements())
       {
           String headername=headernames.nextElement();
           sb.append(headername+" : "+req.getHeader(headername));
           sb.append("<br>");
       }

        resp.getWriter().write(sb.toString());
    }
}
